package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController("userAddressBook")
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿接口")
@Slf4j
public class AddressBookController {

    private final AddressBookMapper addressBookService;

    @Autowired
    public AddressBookController(AddressBookMapper addressBookService) {
        this.addressBookService = addressBookService;
    }


    /**
     * 新增地址
     *
     * @param addressBook 地址博
     * @return Result
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result<?> save(@RequestBody AddressBook addressBook) {
        log.info("执行 新增地址 接口: {}",addressBook);
        addressBookService.saveAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param addressId 地址id
     * @return Result
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<?> deleteById(@RequestParam("id") Long addressId) {
        log.info("执行 根据id删除地址 接口: {}",addressId);
        addressBookService.deleteAddressById(addressId);
        return Result.success();
    }

    /**
     * 根据id修改地址
     *
     * @param addressBook 地址博
     * @return Result
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result<?> update(@RequestBody AddressBook addressBook) {
        log.info("执行 根据id修改地址 接口:{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }


    /**
     * 设置默认地址
     *
     * @param addressBook 地址博
     * @return Result
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result<?> setDefault(@RequestBody AddressBook addressBook) {
        log.info("执行 设置默认地址 接口: {}",addressBook);
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }


    /**
     * 查询当前登录用户的所有地址信息
     *
     * @return 地址博集合
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list() {
        log.info("执行 查询当前登录用户的所有地址信息 接口");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.listAddress(addressBook);
        return Result.success(list);
    }


    /**
     *  根据id查询地址
      * @param addressId 地址id
     * @return Result
     */
    @GetMapping("/{addressId}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long addressId) {
        log.info("执行 根据id查询地址 接口: {}",addressId);
        AddressBook addressBook = addressBookService.getAddressById(addressId);
        return Result.success(addressBook);
    }


    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault() {
        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.listAddress(addressBook);

        if (list != null && list.size() == 1) {
            return Result.success(list.get(0));
        }

        return Result.error("没有查询到默认地址");
    }

}
